#include "../include/head.h"
// 将数据写入文件
void write_flight_file(f_list *list)
{
    if (NULL == list)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }
    if (NULL == list->next)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }
    FILE *fp = fopen("/home/student/code/project/data/flights.txt", "w");
    if (NULL == fp)
    {
        ERRLOG("文件打开失败");
    }
    f_node *ptr = list->next;
    do
    {
        fprintf(fp, "%d\t%s\t%s\t%s\t%02d:%02d\t%02d:%02d\t%s\t%d\t%d\t%d\t%c\n",
                ptr->data.id, ptr->data.flighttype, ptr->data.startcity, ptr->data.arrivecity,
                ptr->data.starttime[0], ptr->data.starttime[1], ptr->data.arrivetime[0], ptr->data.arrivetime[1],
                ptr->data.flytime, ptr->data.value, ptr->data.maxNUM, ptr->data.leftbuyersa, ptr->data.whetherfly); // 写入文件
        ptr = ptr->next;
    } while (list->next != ptr);
    fclose(fp);
}

// 将文件中的数据读入链表
void read_flight_file(f_list *list)
{
    if (NULL == list)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }
    FILE *fp = fopen("/home/student/code/project/data/flights.txt", "r");
    if (NULL == fp)
    {
        ERRLOG("文件打开失败");
    }
    f_node *ptr = list->next;
    while (fscanf(fp, "%d\t%s\t%s\t%s\t%d:%d\t%d:%d\t%s\t%d\t%d\t%d\t%c\n",
                  &ptr->data.id, ptr->data.flighttype, ptr->data.startcity, ptr->data.arrivecity,
                  &ptr->data.starttime[0], &ptr->data.starttime[1], &ptr->data.arrivetime[0], &ptr->data.arrivetime[1],
                  ptr->data.flytime, &ptr->data.value, &ptr->data.maxNUM, &ptr->data.leftbuyersa, &ptr->data.whetherfly) != EOF) // 读取文件
    {
        ptr = ptr->next;
    }
    fclose(fp);
}

// // 读取文件函数
// void read_flight_file(f_list *list)
// {
//     if (NULL == list)
//     {
//         ERRLOG("双向循环链表 为 NULL");
//         return;
//     }

//     FILE *fp = fopen("/home/student/code/project/data/flights.txt", "r");
//     if (NULL == fp)
//     {
//         ERRLOG("文件打开失败");
//         return;
//     }

//     char line[256];
//     while (fgets(line, sizeof(line), fp))
//     {
//         f_node *new_node = (f_node *)malloc(sizeof(f_node));
//         if (NULL == new_node)
//         {
//             ERRLOG("内存分配失败");
//             fclose(fp);
//             return;
//         }

//         // 解析每一行数据
//         sscanf(line, "%d\t%s\t%s\t%s\t%02d:%02d\t%02d:%02d\t%s\t%d\t%d\t%d\t%c",
//                &new_node->data.id, new_node->data.flighttype, new_node->data.startcity, new_node->data.arrivecity,
//                &new_node->data.starttime[0], &new_node->data.starttime[1], &new_node->data.arrivetime[0], &new_node->data.arrivetime[1],
//                new_node->data.flytime, &new_node->data.value, &new_node->data.maxNUM, &new_node->data.leftbuyersa, &new_node->data.whetherfly);

//         // 将新节点插入链表
//         if (list->next == NULL)
//         {
//             list->next = new_node;
//             list->prev = new_node;
//             new_node->next = new_node;
//             new_node->prev = new_node;
//         }
//         else
//         {
//             new_node->next = list->next;
//             new_node->prev = list->prev;
//             list->prev->next = new_node;
//             list->prev = new_node;
//             list->next->prev = new_node;
//         }
//     }

//     fclose(fp);
// }